package com.shadow.istio.server.service;

import com.shadow.istio.api.HelloWorldGrpc;
import com.shadow.istio.api.HelloWorldService;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 服务端业务逻辑实现
 */
@Slf4j
@Component
public class HelloServiceImpl extends HelloWorldGrpc.HelloWorldImplBase {
    @Override
    public void sayHello(HelloWorldService.HelloRequest request,
                         StreamObserver<HelloWorldService.HelloResponse> responseObserver) {
        // 根据请求对象建立响应对象，返回响应信息
        HelloWorldService.HelloResponse response = HelloWorldService.HelloResponse
                .newBuilder()
                .setMessage(String.format("Hello, %s. This message comes from gRPC. v2, ", request.getName()))
                .build();
        responseObserver.onNext(response);  // 返回数据
        responseObserver.onCompleted(); // 结束请求
        log.info("Client Message Received：[{}]", request.getName());
    }
}